
import java.awt.CardLayout;
import java.util.ArrayList;
import javax.swing.JPanel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Comedy Option
 */
public class BuildingView extends JPanel{
    private ArrayList<FloorView> floorViews;
    private Building model;
    CardLayout floorLayout;
    
    public BuildingView(Building toModel){
        floorViews = new ArrayList<FloorView>();
        floorLayout = new CardLayout();
        this.setLayout(floorLayout);
        model = toModel;
        int numFloors = model.getNumFloors();
        for(int i = 0; i < numFloors; i++){
            FloorView newView = new FloorView(model.getFloorByIndex(i));
            floorViews.add(newView);
            this.add(newView, newView.getFloor().getName());
        }
    }
    
    public void addFloor(String floorName){
        Floor newFloor = new Floor(floorName);
        if(model.addFloor(newFloor)){
            FloorView newFloorView = new FloorView(newFloor);   
            floorViews.add(newFloorView);
            floorLayout.addLayoutComponent(newFloorView, floorName);
        }        
    }
    
    public void addRoom(String floor, String roomName, double cost, int beds,
            int x, int y, int width, int height){
        Room toAdd = new Room(roomName, cost, beds);
        if(model.addRoom(floor, toAdd)){
            FloorView toUpdate = findViewByFloorName(floor);
            toUpdate.addRoomView(new RoomView(toAdd,width, height, x, y));
        }
    }
    
    public FloorView findViewByFloorName(String name){
       FloorView toCheck;
       for(int i = 0; i < floorViews.size(); i++){
            toCheck = floorViews.get(i);
            if(toCheck.getFloor().getName().compareTo(name) == 0){
                return toCheck;
            }
        }
        return null;
    }
}
